package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Bill;
import cn.marinda.resturants.entity.Orders;
import cn.marinda.resturants.enums.EntityJson;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.OrderService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.IOUtils;
import cn.marinda.resturants.utils.PagesResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/** 订单控制器
 *
 * @version 1.0
 * @author Marinda
 * @date 2021/12/2
 *
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrderService orderService;
    /** 获取所有订单列
     *
     * @date 2021/12/2
     *
     *
     * @return 订单列表
     */
    @RequestMapping("/queryList")
    private ApiResult queryList()
    {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,orderService.queryList());
    }


    /** 添加
     *
     * @param orders 订单项
     * @return 处理结果
     */
    @RequestMapping("/add")
    private ApiResult addOrders(Orders orders){
        return orderService.addOrder(orders) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /** 删除
     *
     * @param  uid 订单项
     * @return 处理结果
     */
    @RequestMapping("/delete")
    private ApiResult deleteOrders(String uid){
        boolean flag = orderService.deleteOrder(uid);
        return flag ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/pages")
    private PagesResult selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit)
    {
        IPage<Orders> orderIPage = orderService.selectOrderPage(new Pages<>(page,limit));
        return new PagesResult(0,orderIPage.getRecords(),(int)orderIPage.getTotal(),orderIPage.getSize());
    }

    /** 修改
     *
     * @param orders 订单列
     * @return 处理结果
     */
    @RequestMapping("/update")
    private ApiResult updateOrders(Orders orders)
    {
        return orderService.updateOrder(orders) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }


    /**
     * 搜索指定台号的订单信息
     *
     * @param uid 台号id
     * @return 订单信息
     */

    @RequestMapping("/index")
    public ApiResult selectUid(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(orderService.selectUid(uid)));
    }

    /**
     * 选择id
     *
     * @param id id
     * @return {@link ApiResult}
     */
    @RequestMapping("/id")
    public ApiResult selectId(String id) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,orderService.selectId(id));
    }

    /**
     * 通过订单uid找到用户id
     *
     * @param uid 用户uid
     * @return 订单信息
     */
    @RequestMapping("/uid")
    public ApiResult selectOrderUid(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,orderService.selectOrderUid(uid));
    }

    /**
     * 查询订单开始时间和订单结束时间的区间订单列
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 列表订单
     */
    @RequestMapping("/dateInterval")
    public ApiResult selectStartDateAndEndDateInterval(String startDate, String endDate) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,orderService.selectStartDateAndEndDateInterval(startDate, endDate));
    }

    /**
     * 查询这个用户的所有订单项
     *
     * @param user_uid userUid
     * @return 订单列
     */
    @RequestMapping("/userOrderList")
    public  ApiResult selectUserOrderList(String user_uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,orderService.selectUserOrderList(user_uid));

    }

    /**
     * 查询指定uuid的订单的总消费金额
     *
     * @param uid uid
     * @return 总消费金额
     */
    @RequestMapping("/orderNumber")
    public ApiResult selectOrderNumber(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,orderService.selectOrderNumber(uid));

    }
}
